package com.sun.base.producer;

import ch.qos.logback.core.util.TimeUtil;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
 * 发送同步消息
 */
public class SyncProducer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //1、创建消息生产者producer，并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2、指定Nameserver地址
        producer.setNamesrvAddr("192.168.32.131:9876;192.168.32.132:9876");
        //3、启动producer
        producer.start();
        for (int i = 0; i < 10; i++) {
            //4、创建消息对象，指定主题Topic、Tag和消息体
            /**
             * 参数一：消息的主题Topic
             * 参数二：消息的Tag
             * 参数三：消息内容
             */
            Message msg = new Message("base","Tag1",("Hello World"+i).getBytes(StandardCharsets.UTF_8));
            //5、发送消息
            SendResult result = producer.send(msg);
            System.out.println("发送详情:"+result);
            //线程睡一秒
            TimeUnit.SECONDS.sleep(1);
        }
        //6、关闭生产者producer
        producer.shutdown();
    }
}
